define(function (require, exports, module) {
    var jQuery = require('jquery');
// jQuery Alert Dialogs Plugin
//
// Version 1.0
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 29 December 2008
//
// This is an altered version form Aurélien Malisart <aurelien.malisart@gmail.com>
//
// Visit http://github.com/aurels/jquery.alerts
// Visit http://abeautifulsite.net/notebook/87 for more information
//
    (function($) {

        $.alerts = {
            //define the img that might be use
            img_red:"",
            img_green:"",
            // These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time

            verticalOffset: -75,                // vertical offset of the dialog from center screen, in pixels
            horizontalOffset: 0,                // horizontal offset of the dialog from center screen, in pixels/
            repositionOnResize: true,           // re-centers the dialog on window resize
            overlayOpacity: .21,                // transparency level of overlay
            overlayColor: '#FFF',               // base color of overlay
            draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
            okButton: '&nbsp;确定&nbsp;',         // text for the OK button (not used anymore in this version)
            cancelButton: '&nbsp;取消&nbsp;',    // text for the Cancel button (not used anymore in this version)
            dialogClass: null,                  // if specified, this class will be applied to all dialogs
            displaying:false,                   //displaying or not
            queue:[],                            //queue for store alert function
            // Public methods
            alert: function(message, ok, callback) {
                var work = (function (message, ok, callback) {
                   return function(){
                        $.alerts._show(message, null, ok, null, 'alert', function(result) {
                            if( callback ) callback(result);
                        });
                   }
                })(message, ok, callback);

                if( $.alerts.displaying == false && $.alerts.queue.length == 0){
                    work();
                }else {
                    $.alerts.queue.push(work);
                }
            },


            timeout: function(message, ok, callback,second) {


                var work = (function (message, ok, callback,second) {
                    return function(){
                        var count = second;
                        var pri_timer = setInterval(function () {
                            "use strict";
                            --count;

                            if (count == -1) {
                                if ($("#popup_ok").length != 0) $("#popup_ok").click();
                                clearInterval(pri_timer);
                            } else {
                                $("#popup_count").text("(" + count + ")");
                            }

                        }, 1000);
                        $.alerts._show(message, null, ok, null, 'timeout', function (result) {
                            if (callback) callback(result);
                            (function () {
                                clearInterval(pri_timer);
                            })();
                        }, second);
                    }
                })(message, ok, callback,second);



                if( $.alerts.displaying == false && $.alerts.queue.length == 0) {
                    work();
                }else {
                    $.alerts.queue.push(work);
                }
            },

            confirm: function(message, ok, cancel, callback) {


                var work = (function (message, ok,cancel, callback) {
                    return function(){
                        $.alerts._show(message, null, ok, cancel, 'confirm', function(result) {
                            if( callback ) callback(result);

                        });

                    }
                })(message, ok,cancel, callback);

                if( $.alerts.displaying == false && $.alerts.queue.length == 0){
                    work();
                }else {
                    $.alerts.queue.push(work);
                }
            },

            prompt: function(message, value, ok, cancel, callback) {


                var work = (function (message,value, ok,cancel, callback) {
                    return function(){
                        $.alerts._show(message, value, ok, cancel, 'prompt', function(result) {
                            if( callback ) callback(result);
                        });

                    }
                })(message,value, ok,cancel, callback);

                if( $.alerts.displaying == false && $.alerts.queue.length == 0){
                    work();
                }else {
                    $.alerts.queue.push(work);
                }
            },

            // Private methods
            _next:function(){
                "use strict";
                $.alerts.displaying = false;
                if($.alerts.queue.length > 0){
                    $.alerts.queue.shift()();
                }
            },
            _show: function(msg, value, ok, cancel, type, callback,count) {
                $.alerts.displaying = true;
                $.alerts._hide();
                $.alerts._overlay('show');

                $("body").append(
                    '<div id="popup_container">' +
                    '<div id="popup_content">' +
                    '<div id="popup_message"></div>' +
                    '</div>' +
                    '</div>');

                if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass);

                // IE6 Fix
                var pos = ($.browser && $.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed';

                $("#popup_container").css({
                    position: pos,
                    zIndex: 99999,
                    padding: 0,
                    margin: 0
                });

                $("#popup_content").addClass(type);
                $("#popup_message").text(msg);
                $("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br />') );

                $("#popup_container").css({
                    minWidth: $("#popup_container").outerWidth(),
                    maxWidth: $("#popup_container").outerWidth()
                });

                $.alerts._reposition();
                $.alerts._maintainPosition(true);
                switch( type ) {
                    case 'alert':
                        $("#popup_message").after('<div id="popup_panel"><a id="popup_ok" >' + ok + '</a></div>');
                        $("#popup_ok").click( function() {
                            $.alerts._hide();
                            $.alerts._next();
                            callback(true);
                        });
                        $("#popup_ok").focus().keypress( function(e) {
                            if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click');
                        });
                        break;
                    case 'timeout':
                        $("#popup_message").after('<div id="popup_panel"><a id="popup_ok" >' + ok + '<span id="popup_count">(' + count + ')</span></a></div>');
                        $("#popup_ok").click( function() {
                            $.alerts._hide();
                            $.alerts._next();
                            callback(true);
                        });
                        $("#popup_ok").focus().keypress( function(e) {
                            if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click');
                        });
                        break;

                    case 'confirm':
                        $("#popup_message").after('<div id="popup_panel"><a  id="popup_ok" >' + ok + '</a> <a id="popup_cancel" >' + cancel + '</a></div>');
                        $("#popup_ok").click( function() {
                            $.alerts._hide();
                            $.alerts._next();
                            if( callback ) callback(true);
                        });
                        $("#popup_cancel").click( function() {
                            $.alerts._hide();
                            $.alerts._next();

                            if( callback ) callback(false);
                        });
                        // $("#popup_ok").focus();
                        $("#popup_ok, #popup_cancel").keypress( function(e) {
                            if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
                            if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
                        });
                        break;
                    case 'prompt':
                        $("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><a id="popup_ok" >' + ok + '</a> <a id="popup_cancel" >' + cancel + '</a></div>');
                       // $("#popup_prompt").width($("#popup_message").width() );
                        $("#popup_ok").click( function() {
                            var val = $("#popup_prompt").val();
                            $.alerts._hide();
                            $.alerts._next();
                            if( callback ) callback( val );
                        });
                        $("#popup_cancel").click( function() {
                            $.alerts._hide();
                            $.alerts._next();
                            if( callback ) callback( null );
                        });
                        $("#popup_prompt, #popup_ok, #popup_cancel").keypress( function(e) {
                            if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
                            if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
                        });
                        if( value ) $("#popup_prompt").val(value);
                        $("#popup_prompt").focus().select();
                        break;
                }

                // Make draggable
                // if( $.alerts.draggable ) {
                // 				try {
                // 					//$("#popup_container").draggable({ handle: $("#popup_title") });
                // 					//$("#popup_title").css({ cursor: 'move' });
                // 				} catch(e) { /* requires jQuery UI draggables */ }
                // 			}
            },

            _hide: function() {
                $("#popup_container").fadeOut().remove();
                $.alerts._overlay('hide');
                $.alerts._maintainPosition(false);

            },

            _overlay: function(status) {
                switch( status ) {
                    case 'show':
                        $.alerts._overlay('hide');
                        $("BODY").append('<div id="popup_overlay"></div>');
                        $("#popup_overlay").css({
                            position: 'absolute',
                            zIndex: 99998,
                            top: '0px',
                            left: '0px',
                            width: '100%',
                            height: $(document).height(),
                            background: $.alerts.overlayColor,
                            opacity: $.alerts.overlayOpacity,
                            display:"none"
                        });
                        break;
                    case 'hide':
                        $("#popup_overlay").remove();

                        break;
                }
            },

            _reposition: function() {


                var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
                var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset;
                if( top < 0 ) top = 0;
                if( left < 0 ) left = 0;

                // IE6 fix
                if( $.browser && $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();

                $("#popup_container").css({
                    top: top + 'px',
                    left: left + 'px',
                    display:"none"
                });
                $("#popup_container").fadeIn();
                $("#popup_overlay").height( $(document).height() );
            },

            _maintainPosition: function(status) {
                if( $.alerts.repositionOnResize ) {
                    switch(status) {
                        case true:
                            $(window).bind('resize', function() {
                                $.alerts._reposition();
                            });
                            break;
                        case false:
                            $(window).unbind('resize');
                            break;
                    }
                }
            }

        };

        // Shortuct functions
        var custom_Alert = function(message, ok, callback) {
            $.alerts.alert("<h2><img src='"+$.alerts.img_green+"'/></h2>"+message, ok ||"确定", callback);
        };

         var custom_error = function(message, ok, callback) {
            $.alerts.alert("<h2><img src='"+$.alerts.img_red+"'/></h2>"+message, ok ||"确定", callback);
        };

        var custom_timeout = function(message, ok, callback,second) {
            $.alerts.timeout("<h2><img src='"+$.alerts.img_green+"'/></h2>"+message, ok ||"确定", callback,second);
        };


        var custom_confirm = function(message, ok, cancel, callback) {
            $.alerts.confirm("<h2 style='font-size: 48px;line-height: 94px;color: #16d0d8;'>NOTICE</h2>"+message,  ok, cancel, callback);
        };

        var custom_prompt = function(message, value, ok, cancel, callback)  {
            $.alerts.prompt("<h2 style='font-size: 48px;line-height: 94px;color: #16d0d8;'>NOTICE</h2>"+message, value, ok, cancel, callback);
        };


        var jalert = {
            alert:custom_Alert,
            timeout:custom_timeout,
            confirm:custom_confirm,
            prompt:custom_prompt,
            error:custom_error

        };

        if (typeof module !== 'undefined' && module) {
            module.exports = jalert;
        }
    })(jQuery);


});